﻿using System;
using System.Collections.Generic;
using System.Text;
using DataTransferObject;
using System.Data;
namespace DataAccessObject
{
    public class ChiTietBoDeDAO
    {
        private string _conn;
        public ChiTietBoDeDAO(String conn)
        {
            this._conn = conn;
        }

        public clsChiTietBoDe CreateChiTietBoDeFromReder(IDataReader reader)
        {
            clsChiTietBoDe ct = new clsChiTietBoDe();
            ct.MaChiTiet = Convert.ToInt32(reader["MaChiTietBoDe"]);
            ct.MaCauHoi = Convert.ToInt32(reader["MaCauHoi"]);
            ct.MaBoDe = Convert.ToInt32(reader["MaBoDe"]);
            return ct;
        }

        public int ThemChiTietBoDe(clsChiTietBoDe ChiTiet)
        {
            clsKetNoi _KetNoi = new clsKetNoi(_conn);
            return _KetNoi.ExcuteNonQuery(ChiTiet, "ChiTietBoDe", "Insert");
        }
        public void CapNhatChiTiet(clsChiTietBoDe ChiTiet)
        {
            clsKetNoi _KetNoi = new clsKetNoi(_conn);
            _KetNoi.ExcuteNonQuery(ChiTiet, "ChiTietBoDe", "Update");
        }

        public void XoaChiTietBoDe(clsChiTietBoDe ChiTiet)
        {
            clsKetNoi _KetNoi = new clsKetNoi(_conn);
            _KetNoi.ExcuteNonQuery(ChiTiet, "ChiTietBoDe", "Delete");
        }

        public List<clsChiTietBoDe> DanhSachChiTietBoDeTheoMaBoDe(int MaBoDe)
        {
            clsKetNoi _KetNoi = new clsKetNoi(_conn);
            _KetNoi.OpenConnection();
            List<clsChiTietBoDe> DanhSachChiTietBoDe = new List<clsChiTietBoDe>();
            IDataReader reader = _KetNoi.ExcuteReader("Select MaChiTietBoDe, MaCauHoi, MaBoDe from ChiTietBoDe Where MaBoDe="+MaBoDe+" ORDER BY MaCauHoi");
            if (reader==null)
            {
                //Báo lỗi Câu Hỏi này không tồn tại câu hỏi con
            }
            else
            {
                CauHoiDAO CauHoiDAO = new CauHoiDAO(_conn);
                while (reader.Read())
                {
                    clsCauHoi CauHoi = new clsCauHoi();
                    clsChiTietBoDe ChiTiet = new clsChiTietBoDe();
                    ChiTiet = CreateChiTietBoDeFromReder(reader);
                    CauHoi = CauHoiDAO.LayCauHoiTheoId(ChiTiet.MaCauHoi);
                    if (CauHoi.LoaiCauHoi == 5)
                    {
                        clsCauHoi CauHoiCha = new clsCauHoi();
                        CauHoiCha = CauHoiDAO.LayCauHoiTheoId(CauHoi.CauHoiCha);
                        ChiTiet.MaCauHoi = CauHoiCha.MaCauHoi;
                        if (KiemTraDanhSachChiTiet(DanhSachChiTietBoDe,ChiTiet.MaCauHoi) == false)
                        {
                            DanhSachChiTietBoDe.Add(ChiTiet);
                        }
                    }
                    else
                    {
                        DanhSachChiTietBoDe.Add(ChiTiet);
                    }
                } 
            }
            _KetNoi.CloseConnection();
            return DanhSachChiTietBoDe;
        }

        private bool KiemTraDanhSachChiTiet(List<clsChiTietBoDe> DanhSachChiTiet, int MaCauHoi)
        {
            for (int i = 0; i < DanhSachChiTiet.Count; i++)
            {
                if (DanhSachChiTiet[i].MaCauHoi == MaCauHoi)
                    return true;
            }
            return false;
        }

      
    }

}
